namespace AgentJohnson
{
  using System;
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Feature.Services.CSharp.Bulbs;
  using Resharper.Abstractions.Actions;
  using Resharper.Abstractions.CodeDom.Expressions;
  using Resharper.Abstractions.CodeDom.Expressions.CastOperatorExpressions;

  /// <summary>Defines the <see cref="UseAsOperator"/> class.</summary>
  [ContextAction("Use 'as' operator [Agent Johnson]", "Use 'as' operator.")]
  public class UseAsOperator : ContextActionBase
  {
    #region Constructors and Destructors

    /// <summary>Initializes a new instance of the <see cref="UseAsOperator"/> class.</summary>
    /// <param name="provider">The provider.</param>
    public UseAsOperator([NotNull] ICSharpContextActionDataProvider provider) : base(provider)
    {
    }

    #endregion

    #region Methods

    /// <summary>Determines whether this instance can execute the specified action context.</summary>
    /// <param name="context">The action context.</param>
    /// <returns><c>true</c> if this instance can execute the specified action context; otherwise, <c>false</c>.</returns>
    protected override bool CanExecute(CanExecuteContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      return this.GetModel(context) != null;
    }

    /// <summary>Executes the action.</summary>
    /// <param name="context">The execute context.</param>
    protected override void Execute(ExecuteContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      var model = this.GetModel(context);
      if (model == null)
      {
        return;
      }

      var operand = model.Expression.Operand;
      if (operand == null)
      {
        return;
      }

      var typeOperand = model.Expression.TypeOperand;
      var text = operand.ToString();

      var expression = context.Factory.CreateExpression(string.Format("{0} as {1}", text, typeOperand));

      model.Expression.ReplaceBy(expression);
    }

    /// <summary>Gets the text.</summary>
    /// <returns>Returns the text.</returns>
    protected override string GetText()
    {
      return "Use 'as' operator [Agent Johnson]";
    }

    /// <summary>Gets the model.</summary>
    /// <param name="context">The context.</param>
    /// <returns>Returns the model.</returns>
    private Model GetModel(ProviderContext context)
    {
      var expression = context.GetSelectedCastOperatorExpression();
      if (expression == null)
      {
        return null;
      }

      if (!expression.OperatorContains(context.CaretOffset))
      {
        return null;
      }

      if (expression.Operand == null)
      {
        return null;
      }

      return new Model
      {
        Expression = expression,
      };
    }

    #endregion

    /// <summary>Defines the <see cref="Model"/> class.</summary>
    private class Model
    {
      #region Properties

      /// <summary>
      /// Gets or sets the expression.
      /// </summary>
      /// <value>The expression.</value>
      [NotNull]
      public CastOperatorExpression Expression { get; set; }

      #endregion
    }
  }
}